﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data.SqlTypes;
using MySql.Data.MySqlClient;

namespace DAL.DHelpers
{

    public class ParamsCreator
    {
        public static ParamsCreator instance = new ParamsCreator();
        
        public MySqlParameter[] GetParams(List<String> parNames, List<object> parValues)
            {
                int n =parValues.Count;
                MySqlParameter[] p = new MySqlParameter[n];
                for (int i = 0; i < n;i++ )
                    p[i] = new MySqlParameter(parNames[i].ToString(), parValues[i]);
                return p;
            }
        public MySqlParameter[] GetParams(string[] parNames, object[] parValues)
        {
            int n = parValues.Length;
            MySqlParameter[] p = new MySqlParameter[n];
            for (int i = 0; i < n; i++)
                p[i] = new MySqlParameter(parNames[i].ToString(), parValues[i]);
            return p;
        }
        public MySqlParameter[] GetInsertParams(string[] parNames, string[] Cols, object[] parValues)
        {
            int n = parNames.Length;
            MySqlParameter[] p = new MySqlParameter[n];
            for (int i = 0; i < n; i++)
                p[i] = new MySqlParameter( parNames[i].ToString(), null);
            for (int j = 0; j < Cols.Length; j++)
                {
                for (int i = 0; i < n; i++)
                    if (Cols[j].Equals(parNames[i]))
                        {
                            p[i] = new MySqlParameter( parNames[i].ToString(), parValues[j]);
                        }
                }
            return p;
        }
        public MySqlParameter[] GetParams(string Names, string Values, string Types)
        {
            string[] parValues = Values.Split(',');
            string[] parNames = Names.Split(',');
            string[] typeValues = Types.Split(',');
            object[] p = new object[parNames.Length];
            for (int i = 0; i < parNames.Length; i++)
                p[i] = GetValue(ConvertFromString(typeValues[i]), parValues[i]);
           return GetParams(parNames, parValues);
        }
        public MySqlParameter[] GetParams(Column[] Values)
        {
            int n = Values.Length;
            MySqlParameter[] p = new MySqlParameter[n];
            for (int i = 0; i < n ; i++)
            {
                p[i] = new MySqlParameter(Values[i].ColumnName, Values[i].GetValues());
            }
            
            return p;
        }
        public MySqlParameter[] GetParams(MySqlParameter[] myParams1, MySqlParameter[] myParams2)
        {
            int j = 0;
            MySqlParameter[] myParams = new MySqlParameter[myParams1.Length + myParams2.Length];
            for (int i = 0; i< myParams1.Length; i++)
            {
                myParams[j] = myParams1[i];
                j++;
            }
            for (int i = 0; i< myParams2.Length; i++)
            {
                myParams[j] = myParams2[i];
                j++;
            }
            return myParams;
        }
        public String GetSeparateString(string [] array)
            {
                String str = String.Empty;
                int i = 0;
            for(;i<array.Length-1;i++)
                {
                    
                    str += array[i] + ",";
                }
            str += array[i];
            return str;
            }
       
        public DataType.DataTypeSql ConvertFromString(String type)
        {
            DAL.DHelpers.DataType.DataTypeSql typeSQl = DAL.DHelpers.DataType.DataTypeSql.Default;
            switch (type)
            {
                case "varchar":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Varchar;
                    break;
                case "nvarchar":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Nvarchar;
                    break;
                case "datetime":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Datetime;
                    break;
                case "smallint":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Smallint;
                    break;
                case "int":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Int;
                    break;
                case "char":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Char;
                    break;
                case "bit":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Bit;
                    break;
                case "bigint":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Bigint;
                    break;
                case "ntext":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Ntext;
                    break;
                case "decimal":
                    typeSQl = DAL.DHelpers.DataType.DataTypeSql.Decimal;
                    break;
            }
            return typeSQl;

        }
        public object GetValue(DataType.DataTypeSql type, string value)
        {

            object objValue = null;
            switch (type)
            {
                case DHelpers.DataType.DataTypeSql.Smallint:
                    objValue = String.IsNullOrEmpty(value) ? SqlInt16.Null : new SqlInt16(Convert.ToInt16(value));
                    break;
                case DHelpers.DataType.DataTypeSql.Int:
                    objValue = String.IsNullOrEmpty(value) ? SqlInt32.Null : new SqlInt32(Convert.ToInt32(value));
                    break;
                case DHelpers.DataType.DataTypeSql.Bigint:
                    objValue = String.IsNullOrEmpty(value) ? SqlInt64.Null : new SqlInt64(Convert.ToInt64(value));
                    break;
                case DHelpers.DataType.DataTypeSql.Bit:
                    objValue = String.IsNullOrEmpty(value) ? SqlBoolean.Null : new SqlBoolean(value == "1" || value.ToLower() == "true" ? true : false);
                    break;
                case DHelpers.DataType.DataTypeSql.Char:
                case DHelpers.DataType.DataTypeSql.Ntext:
                case DHelpers.DataType.DataTypeSql.Nvarchar:
                case DHelpers.DataType.DataTypeSql.Varchar:
                    objValue = String.IsNullOrEmpty(value) ? SqlString.Null : new SqlString(value);
                    break;
                case DHelpers.DataType.DataTypeSql.Datetime:
                    objValue = String.IsNullOrEmpty(value) ? SqlDateTime.Null : new SqlDateTime(Convert.ToDateTime(value));
                    break;
                case DHelpers.DataType.DataTypeSql.Decimal:
                    objValue = String.IsNullOrEmpty(value) ? SqlDecimal.Null : new SqlDecimal(Convert.ToDecimal(value));
                    break;
            }
            return objValue;
        }
    }
}
